﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Numerics;
using ArgusLib.Fractals;
using ArgusLib.Controls;
using ArgusLib.Drawing;
using ArgusLib.Fractals.OrbitFractals;

namespace Fraktalysator
{
	partial class MainForm
	{
		IParameter parameterBackUp;

		void formImage_MouseEnter(object sender, EventArgs e)
		{
			this.tslToolTipValue.Text = Localization.Text.ToolTip_ImageForm;
		}

		void formImage_MouseLeave(object sender, EventArgs e)
		{
			this.tslMouseValue.Text = Localization.Text.Label_ReIm;
			this.tslToolTipValue.Text = string.Empty;
		}

		void formImage_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.Fractal == null)
				return;
			if (this.Fractal.DataSet == null)
				return;
			this.tslMouseValue.Text = this.Fractal.DataSet.CalculationParameter.Convert(e.X/this.formImage.Magnification, e.Y/this.formImage.Magnification).ToString();
		}

		void formImage_RectangleSelected(object sender, EventArgs e)
		{
			if (this.Fractal.IsCalculating == true)
				return;

			FractalComplex f = this.Fractal as FractalComplex;
			if (f == null)
				return;

			RectangleA sel = this.formImage.Selection;
			Parameter paraConvert = (Parameter)this.formFractalParameter.InputControl.Parameter;
			ComplexArea area = new ComplexArea();
			if (this.formImage.ZoomDirection == ZoomDirection.ZoomIn)
			{
				area.Width = sel.Width / this.formImage.ImageSize.Width * paraConvert.ShownArea.Width;
				area.Height = sel.Height / this.formImage.ImageSize.Height * paraConvert.ShownArea.Height;
				area.Center = paraConvert.Convert(sel.Center.X, sel.Center.Y);
				area.Rotation = paraConvert.ShownArea.Rotation + sel.Rotation;
			}
			else
			{
				area.Width = paraConvert.ShownArea.Width * this.formImage.ImageSize.Width / sel.Width;
				area.Height = paraConvert.ShownArea.Height * this.formImage.ImageSize.Height / sel.Height;
				area.Center = paraConvert.ShownArea.Center;
				area.Rotation = paraConvert.ShownArea.Rotation + sel.Rotation;
			}
			Parameter para = (Parameter)this.GetFractalParameter();
			para.ShownArea = area;
			this.Fractal.Parameter = para;
			this.StartCalculation(true);
		}

		void formImage_SelectionChanged(object sender, EventArgs e)
		{
			if (this.formPreviewImage.Visible == false)
				return;
			RectangleA selection = this.formImage.Selection;
			if (selection.Size.IsEmpty == true)
				return;

			Image image = this.formPreviewImage.Image;
			this.formPreviewImage.Image = this.formImage.Image.Crop(selection);
			if (image != null)
				image.Dispose();
		}

		void formImage_SelectionChangingBegin(object sender, EventArgs e)
		{
			ArgusLib.Fractals.IParameter parameter = this.formFractalParameter.InputControl.Parameter;
			this.formImage.RectangleRatio = (float)parameter.PixelWidth / parameter.PixelHeight;
		}

		void formImage_ResizeBegin(object sender, EventArgs e)
		{
			if (this.Fractal != null)
				this.parameterBackUp = (IParameter)this.Fractal.Parameter.Clone();
		}

		void formImage_ResizeEnd(object sender, EventArgs e)
		{
			if (this.Fractal == null || this.parameterBackUp == null)
			{
				this.formImage.ImageSize = new Size(this.parameterBackUp.PixelWidth, this.parameterBackUp.PixelHeight);
				this.parameterBackUp = null;
				return;
			}

			Parameter para = this.GetFractalParameter() as Parameter;
			if (para == null)
			{
				this.formImage.ImageSize = new Size(this.parameterBackUp.PixelWidth, this.parameterBackUp.PixelHeight);
				this.parameterBackUp = null;
				return;
			}

			para.PixelWidth = this.formImage.ImageSize.Width;
			para.PixelHeight = this.formImage.ImageSize.Height;
			SizeD size = new SizeD(para.ShownArea.Width, para.ShownArea.Height);
			size = size.ChangeRatio(this.formImage.ImageSize.GetRatio(), ChangeRatioOptions.IncreaseAreaSize);
			ComplexArea area = para.ShownArea;
			area.Width = size.Width;
			area.Height = size.Height;
			para.ShownArea = area;
			this.Fractal.Parameter = para;

			this.StartCalculation(true);
		}

		void formImage_SizeChanged(object sender, EventArgs e)
		{
			if (this.Fractal == null || this.parameterBackUp != null)
				return;

			if (this.Fractal.Parameter.PixelWidth == this.formImage.ImageSize.Width &&
				this.Fractal.Parameter.PixelHeight == this.formImage.ImageSize.Height)
			{
				return;
			}

			this.parameterBackUp = (IParameter)this.Fractal.Parameter.Clone();

			if (ArgusLib.Environment.Runtime == ArgusLib.Runtime.Mono)
				MessageBox.Show("Halllo");

			this.formImage_ResizeEnd(sender, e);
		}
	}
}
